import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, setRefreshToken, setExpiresIn, removeToken, removeRefreshToken, removeExpiresIn } from '@/utils/auth'
import { resetRouter } from '@/router'


const getDefaultState = () => {
  return {
    token: getToken(),
    // name: '',
    nickname: '',
    avatar: '',
    rolename: '',
    roles: [],
    permissions: [],
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // SET_NAME: (state, name) => {
  //   state.name = name
  // },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
  },
  SET_ROLENAME: (state, roles_name) => {
    state.roles_name = roles_name
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },

  //   commit('SET_ROLENAME', roles_name)
  // commit('SET_PHONE', phone)



  // SET_NICKNAME: (state, nickname) => {
  //   state.nickname = nickname
  // },
  // SET_ROLENAME: (state, rolename) => {
  //   state.rolename = rolename
  // },

  // SET_PHONE: (state, phone) => {
  //   state.phone = phone
  // }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        // console.log('response',response)
        const { data } = response
        // console.log('access_token',data.access_token)
        commit('SET_TOKEN', data.access_token)

        // 获取当前时间+返回的过期时间
        let expires_time = Date.now() + data.expires_in*1000 //data.expires_in*1000过期时间转为毫秒

        // 保存cookie
        setToken(data.access_token)
        setRefreshToken(data.refresh_token)
        setExpiresIn(expires_time)

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  setRefrshToken( { commit }, data ){
    return new Promise(resolve => {
      commit('SET_TOKEN', data.access_token)

      // 获取当前时间+返回的过期时间
      let expires_time = Date.now() + data.expires_in*1000 //data.expires_in*1000过期时间转为毫秒

      // 保存cookie
      setToken(data.access_token)
      setRefreshToken(data.refresh_token)
      setExpiresIn(expires_time)
      resolve();  
    })
  },


  // get user info
  //获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        // console.log('response',response)
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        // console.log(data)
        const { username, nickname, phone, avatar, email, roles, roles_name, permissions } = data
        const name = username

        // roles must be a non-empty array
        if (!roles || roles.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }

        // 保存后台返回的角色和权限功能到vuex
        commit('SET_ROLES', roles)
        // commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_EMAIL', email)
        commit('SET_PERMISSIONS', permissions)
        commit('SET_NICKNAME', nickname)
        commit('SET_ROLENAME', roles_name)
        commit('SET_PHONE', phone)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        // 移除token
        removeToken() // must remove  token  first
        removeRefreshToken()
        removeExpiresIn()
        resetRouter()
        // 


        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

